package com.atguigu.realtime.app.dws;

import com.atguigu.realtime.app.BaseSqlApp;
import com.atguigu.realtime.bean.KeyWordStats;
import com.atguigu.realtime.function.IkAnalyzer;
import com.atguigu.realtime.util.FlinkSinkUtil;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.ZoneOffset;

import static com.atguigu.realtime.commont.Constant.TOPIC_DWD_PAGE;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/3 9:10
 */
public class DwsSearchKeyWordApp extends BaseSqlApp {
    public static void main(String[] args) {
        new DwsSearchKeyWordApp().init(4004, 1, "DwsSearchKeyWordApp");
        
    }
    
    @Override
    protected void run(StreamTableEnvironment tenv) {
        tenv.getConfig().setLocalTimeZone(ZoneOffset.ofHours(8));
        // 1. 建一张动态表与topic关联
        tenv.executeSql("create table page_log(" +
                            "   page map<string, string>, " +
                            "   ts bigint, " +
                            "   et as to_timestamp(from_unixtime(ts/1000)), " +
                            "   watermark for et as et - interval '3' second " +
                            ")with(" +
                            "   'connector' = 'kafka'," +
                            "   'properties.bootstrap.servers' = 'hadoop162:9092,hadoop163:9092, hadoop164:9092'," +
                            "   'properties.group.id' = 'DwsSearchKeyWordApp'," +
                            "   'topic' = '" + TOPIC_DWD_PAGE + "'," +
                            "   'scan.startup.mode' = 'latest-offset', " +
                            "   'format' = 'json' " +
                            ")");
        
        // 2. 过滤出需要的数据(商品列表)
        Table t1 = tenv.sqlQuery("select" +
                                     " page['item'] word, " +
                                     " et " +
                                     "from page_log " +
                                     "where page['page_id']='good_list' " +
                                     "and page['item_type']='keyword' " +
                                     "and page['item'] is not null");
        tenv.createTemporaryView("t1", t1);
        // 3. 列转行(使用制表函数)
        // 3.1 先注册自定义函数
        tenv.createTemporaryFunction("ik_analyzer", IkAnalyzer.class);
        // 3.2 使用自定义函数实现列转行
        Table t2 = tenv.sqlQuery("select" +
                                     " keyword, " +
                                     " et " +
                                     "from t1 " +
                                     "join lateral table(ik_analyzer(word)) on true");
        tenv.createTemporaryView("t2", t2);
        
        // 4. 开窗聚合
        Table result = tenv.sqlQuery("select" +
                                        " date_format(tumble_start(et, interval '5' second), 'yyyy-MM-dd HH:mm:ss') stt, " +
                                        " date_format(tumble_end(et, interval '5' second), 'yyyy-MM-dd HH:mm:ss') edt, " +
                                        " keyword, " +
                                        " 'search' source, " +
                                        " count(*) ct, " +
                                        " unix_timestamp() * 1000 ts " +
                                        "from t2 " +
                                        "group by " +
                                        " tumble(et, interval '5' second), " +
                                        " keyword");
    
        // 5. 写入到ClickHouse中
        tenv
            .toRetractStream(result, KeyWordStats.class)
            .filter(t -> t.f0)
            .map(t -> t.f1)
            .addSink(FlinkSinkUtil.getClickHouseSink("gmall2021", "keyword_stats_2021", KeyWordStats.class));
        
    }
}
